#include "lightsourcedialog.h"
#include "ui_lightsourcedialog.h"
#include <QSettings>
#include <QDebug>
#include <QValidator>
#include <QTimer>

#include <QXmlStreamWriter>
#include <QXmlStreamReader>

#include "toast.h"
#include "serialportmanager.h"
#include "bgapackagemanager.h"


LightSourceDialog::LightSourceDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LightSourceDialog)
{
    ui->setupUi(this);

	setWindowFlags(Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog);

	PACKAGE_INFO info = StaticFunction::GetPackageInfo();
	

    QStringList list = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
	QString appName = QCoreApplication::applicationName();
	QSettings app(appName, "Camera");
    QIntValidator* aIntValidator = new QIntValidator;

    ui->lineEditRed->setValidator(aIntValidator);
    int redBrightness = app.value("Red").toInt();
    ui->horizontalSliderRed->setValue(redBrightness);


	QSerialPort *m_serialLight = SerialPortManager::getInstance()->GetLightCom();
    QObject::connect(m_serialLight, &QSerialPort::readyRead, this, [=]() {
        static QByteArray dataBuffer;
        QByteArray bytes;
        bytes = m_serialLight->readAll();
		//qDebug() << "read:" << bytes;
		for (int i = 0; i < bytes.length(); i++)
		{
			if (bytes[i] == 'A' || bytes[i] == 'B' || bytes[i] == 'C' || bytes[i] == 'D')
			{
				bytes.remove(i, 1);
				i--;
			}
		}

        if (!bytes.isEmpty())
        {
			
			dataBuffer += bytes;
			//qDebug() << "data:" << dataBuffer;
			if (dataBuffer.length() == 5)
			{
				if (dataBuffer[0] == 'b')
				{
					int value = (dataBuffer[1] - '0') * 1000 + (dataBuffer[2] - '0') * 100 + (dataBuffer[3] - '0') * 10 + (dataBuffer[4] - '0');
					ui->horizontalSliderRed->setValue(value);
					ui->lineEditRed->setText(QString::number(value));
					dataBuffer.clear();
				}
				else if (dataBuffer[0] == 'c')
				{
					int value = (dataBuffer[1] - '0') * 1000 + (dataBuffer[2] - '0') * 100 + (dataBuffer[3] - '0') * 10 + (dataBuffer[4] - '0');
					ui->horizontalSliderBlue->setValue(value);
					ui->lineEditBlue->setText(QString::number(value));
					dataBuffer.clear();
				}
				else if (dataBuffer[0] == 'd')
				{
					int value = (dataBuffer[1] - '0') * 1000 + (dataBuffer[2] - '0') * 100 + (dataBuffer[3] - '0') * 10 + (dataBuffer[4] - '0');
					ui->horizontalSliderWhite->setValue(value);
					ui->lineEditWhite->setText(QString::number(value));
					dataBuffer.clear();
				}
				else if (dataBuffer[0] == 'a')
				{
					int value = (dataBuffer[1] - '0') * 1000 + (dataBuffer[2] - '0') * 100 + (dataBuffer[3] - '0') * 10 + (dataBuffer[4] - '0');
					ui->horizontalSlider4->setValue(value);
					ui->lineEdit4->setText(QString::number(value));
					dataBuffer.clear();
				}
			}
        }

    });
    GetLight(LIGHT_RED);
    QTimer::singleShot(150, this, [=]() {
        GetLight(LIGHT_BLUE);
		QTimer::singleShot(150, this, [=]() {
			GetLight(LIGHT_WHITE);
			QTimer::singleShot(150, this, [=]() {
				GetLight(LIGHT_ASSIST);
			});
		});
    });


	connect(ui->horizontalSliderRed, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChangedRed()));
	connect(ui->lineEditRed, SIGNAL(editingFinished()), this, SLOT(EditTextChangedRed()));
	connect(ui->horizontalSliderBlue, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChangedBlue()));
	connect(ui->lineEditBlue, SIGNAL(editingFinished()), this, SLOT(EditTextChangedBlue()));
	connect(ui->horizontalSliderWhite, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChangedWhite()));
	connect(ui->lineEditWhite, SIGNAL(editingFinished()), this, SLOT(EditTextChangedWhite()));
	connect(ui->horizontalSlider4, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged4()));
	connect(ui->lineEdit4, SIGNAL(editingFinished()), this, SLOT(EditTextChanged4()));
    
	connect(ui->btnOn, &QPushButton::clicked, [=]() {
		int value = 50;
		PACKAGE_INFO info = StaticFunction::GetPackageInfo();
		SerialPortManager *sp = SerialPortManager::getInstance();
		sp->SetFourLightBrightnessSync(value, value, value, value);

		ui->horizontalSliderRed->setValue(value);
		ui->lineEditRed->setText(QString::number(value));

		ui->horizontalSliderBlue->setValue(value);
		ui->lineEditBlue->setText(QString::number(value));

		ui->horizontalSliderWhite->setValue(value);
		ui->lineEditWhite->setText(QString::number(value));

		ui->horizontalSlider4->setValue(value);
		ui->lineEdit4->setText(QString::number(value));
		
	});
	connect(ui->btnOff, &QPushButton::clicked, [=]() {
		int value = 0;
		PACKAGE_INFO info = StaticFunction::GetPackageInfo();
		SerialPortManager *sp = SerialPortManager::getInstance();
		sp->SetFourLightBrightnessSync(value, value, value, value);

		ui->horizontalSliderRed->setValue(value);
		ui->lineEditRed->setText(QString::number(value));

		ui->horizontalSliderBlue->setValue(value);
		ui->lineEditBlue->setText(QString::number(value));

		ui->horizontalSliderWhite->setValue(value);
		ui->lineEditWhite->setText(QString::number(value));

		ui->horizontalSlider4->setValue(value);
		ui->lineEdit4->setText(QString::number(value));
		
	});

	UpdateUseCount();
	connect(ui->pushButton, &QPushButton::clicked, [=]() {
		QSettings app(appName, "config");
		app.setValue("redLightCount", 0);
		UpdateUseCount();
	});
	connect(ui->pushButton_2, &QPushButton::clicked, [=]() {
		QSettings app(appName, "config");
		app.setValue("blueLightCount", 0);
		UpdateUseCount();
	});
	connect(ui->pushButton_3, &QPushButton::clicked, [=]() {
		QSettings app(appName, "config");
		app.setValue("whiteLightCount", 0);
		UpdateUseCount();
	});
	connect(ui->pushButton_4, &QPushButton::clicked, [=]() {
		QSettings app(appName, "config");
		app.setValue("white2LightCount", 0);
		UpdateUseCount();
	});
}

LightSourceDialog::~LightSourceDialog()
{
    delete ui;
}

void LightSourceDialog::UpdateUseCount()
{
	QString appName = QCoreApplication::applicationName();
	QSettings app(appName, "config");
	int value1 = app.value("redLightCount").toInt();
	ui->label->setText(QString("Usage Counter: %1").arg(value1));
	value1 = app.value("blueLightCount").toInt();
	ui->label_2->setText(QString("Usage Counter: %1").arg(value1));
	value1 = app.value("whiteLightCount").toInt();
	ui->label_3->setText(QString("Usage Counter: %1").arg(value1));
	value1 = app.value("white2LightCount").toInt();
	ui->label_4->setText(QString("Usage Counter: %1").arg(value1));
}

void LightSourceDialog::GetLight(LIGHT_TYPE type)
{
	SerialPortManager *sp = SerialPortManager::getInstance();

	if (type == LIGHT_RED)
	{
		sp->GetLightBrightnessB();
	}
	else if (type == LIGHT_BLUE)
	{
		sp->GetLightBrightnessC();
	}
	else if (type == LIGHT_WHITE)
	{
		sp->GetLightBrightnessD();
	}
	else {
		sp->GetLightBrightnessA();
	}
}

void LightSourceDialog::SetLight(int light, LIGHT_TYPE type)
{
	SerialPortManager *sp = SerialPortManager::getInstance();

	if (type == LIGHT_RED)
	{
		sp->SetLightBrightnessB(light);
	}
	else if (type == LIGHT_BLUE)
	{
		sp->SetLightBrightnessC(light);
	}
	else if (type == LIGHT_WHITE)
	{
		sp->SetLightBrightnessD(light);
	}
	else {
		sp->SetLightBrightnessA(light);
	}
}

void LightSourceDialog::SliderValueChangedRed()
{
    QSlider *slider = (QSlider *)sender();
    QString value = QString::number(slider->value());
    ui->lineEditRed->setText(value);

    SetLight(slider->value(), LIGHT_RED);
}

void LightSourceDialog::EditTextChangedRed()
{
    QLineEdit *edit = (QLineEdit *)sender();
    int v = edit->text().toInt();
    ui->horizontalSliderRed->setValue(v);

    SetLight(v, LIGHT_RED);
}

void LightSourceDialog::SliderValueChangedBlue()
{
	QSlider *slider = (QSlider *)sender();
	QString value = QString::number(slider->value());
	ui->lineEditBlue->setText(value);

	SetLight(slider->value(), LIGHT_BLUE);
}

void LightSourceDialog::EditTextChangedBlue()
{
	QLineEdit *edit = (QLineEdit *)sender();
	int v = edit->text().toInt();
	ui->horizontalSliderBlue->setValue(v);

	SetLight(v, LIGHT_BLUE);
}
void LightSourceDialog::SliderValueChangedWhite()
{
	QSlider *slider = (QSlider *)sender();
	QString value = QString::number(slider->value());
	ui->lineEditWhite->setText(value);

	SetLight(slider->value(), LIGHT_WHITE);
}

void LightSourceDialog::EditTextChangedWhite()
{
	QLineEdit *edit = (QLineEdit *)sender();
	int v = edit->text().toInt();
	ui->horizontalSliderWhite->setValue(v);

	SetLight(v, LIGHT_WHITE);
}

void LightSourceDialog::SliderValueChanged4()
{
	QSlider *slider = (QSlider *)sender();
	QString value = QString::number(slider->value());
	ui->lineEdit4->setText(value);

	SetLight(slider->value(), LIGHT_ASSIST);
}

void LightSourceDialog::EditTextChanged4()
{
	QLineEdit *edit = (QLineEdit *)sender();
	int v = edit->text().toInt();
	ui->horizontalSlider4->setValue(v);

	SetLight(v, LIGHT_ASSIST);
}


void LightSourceDialog::closeEvent(QCloseEvent *)
{
	QSerialPort *m_serialLight = SerialPortManager::getInstance()->GetLightCom();
	disconnect(m_serialLight, &QSerialPort::readyRead, 0, 0);
}
